﻿using CommunityToolkit.Mvvm.Input;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using UpperComputer.Application;
using UpperComputer.WPF.Entry.Common;
using UpperComputer.WPF.Entry.Common.Extensions;
using UpperComputer.WPF.Entry.Views;

namespace UpperComputer.WPF.Entry.ViewModels
{
    public class MenuButtonCopyViewModel : OperationViewModel
    {
        private readonly IBaseMenuButtonService _menuButtonService;

        public IAsyncRelayCommand MenuSelectCommand { get; }

        private string _menuName;
        [Required(ErrorMessage = "请选择菜单")]
        public string MenuName
        {
            get => _menuName;
            set => SetProperty(ref _menuName, value);
        }

        private string _menuCode;

        public List<BaseMenuButtonDto> MenuButtons { get; }

        public ObservableCollection<BaseMenuButtonDto> SelectMenuButtons { get; } = new ObservableCollection<BaseMenuButtonDto>();

        public MenuButtonCopyViewModel(IBaseMenuButtonService menuButtonService)
        {
            _menuButtonService = menuButtonService;
            MenuSelectCommand = new AsyncRelayCommand(MenuSelectCommand_Execute);
            MenuButtons = new List<BaseMenuButtonDto>()
            {
                new BaseMenuButtonDto{ Code = Constant.OPERATION_POST, DisplayName = Lang["新增"], Icon = "FileAddOutlined"},
                new BaseMenuButtonDto{ Code = Constant.OPERATION_PUT, DisplayName = Lang["编辑"], Icon = "EditOutlined"},
                new BaseMenuButtonDto{ Code = Constant.OPERATION_DELETE, DisplayName = Lang["删除"], Icon = "DeleteOutlined" }
            };
        }

        private async Task MenuSelectCommand_Execute(CancellationToken cancellationToken)
        {
            MenuSelectControl menuSelectControl = Furion.App.GetService<MenuSelectControl>();
            object result = await menuSelectControl.ShowCustomDialog(Constant.DIALOG_IDENTIFIER_MENU);
            if (result != null && result is BaseMenuResultDto)
            {
                BaseMenuResultDto menu = (BaseMenuResultDto)result;
                MenuName = menu.DisplayName;
                _menuCode = menu.Code;
            }
        }

        protected override async Task OkAsync(object parameter, CancellationToken cancellationToken)
        {
            if (!IsValidated)
            {
                return;
            }
            if (SelectMenuButtons.Count == 0)
            {
                Error = "请选择按钮";
                return;
            }
            foreach (BaseMenuButtonDto menuButton in SelectMenuButtons)
            {
                menuButton.MenuCode = _menuCode;
            }
            await TryExecuteCommandAysnc(async cancellationToken =>
            {
                await _menuButtonService.Copy(SelectMenuButtons.ToList(), cancellationToken);
                CloseRootDialog(true);
            }, cancellationToken);
        }
    }
}
